#pragma once
#include "../server.hpp"

class EchoServer
{
private:
    TcpServer tcp_server_;

private:
    void OnDisConnect(const SharedptrCon &con)
    {
        DBG_LOG("RELEASE CONNECTION: %p", con.get());
    }

    void OnConnect(const SharedptrCon &con)
    {
        DBG_LOG("CONNECTION CREATE SUCCESS!!");
    }

    void OnMessage(const SharedptrCon &con, Buffer *buf)
    {
        char buffer[65535];
        buf->ReadAndPop(buffer, buf->ReadableSize());
        std::string ret = "这是一个处理结果";
        con->Send(ret.c_str(), ret.size());
    }

public:
    EchoServer(int port) : tcp_server_(port)
    {
    }

    void Start()
    {
        tcp_server_.SetThreadCount(2);
        tcp_server_.EnableInactiveRelease(10);
        tcp_server_.SetConnectCallback(std::bind(&EchoServer::OnConnect, this, std::placeholders::_1));
        tcp_server_.SetMessagetCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        tcp_server_.SetDisconnectCallback(std::bind(&EchoServer::OnDisConnect, this, std::placeholders::_1));
        tcp_server_.Start();
    }
};